package org.summerframework.component.security.oauth.weibo;


import com.alibaba.fastjson.JSONObject;
import org.summerframework.component.httpclient.HttpClientUtil;
import org.summerframework.component.httpclient.HttpParam;
import org.summerframework.component.httpclient.HttpResult;
import org.summerframework.component.security.oauth.LoginType;
import org.summerframework.component.security.oauth.OAuthAppIdAndAppSecret;
import org.summerframework.component.security.oauth.OAuthResultCode;
import org.summerframework.component.security.oauth.exception.WeCatException;
import org.springframework.web.bind.annotation.RequestMethod;

public class WeiboOAuthSupper implements WeiboOAuthInterceptor {

    private final static String ACCESS_TOKEN_URI = "https://api.weibo.com/oauth2/access_token";

    private final static String USER_SHOW_URI = "https://api.weibo.com/2/users/show.json";

    private WeiboAppIdAndAppSecret weiboAppIdAndAppSecret;

    public WeiboOAuthSupper(WeiboAppIdAndAppSecret weiboAppIdAndAppSecret) {
        this.weiboAppIdAndAppSecret = weiboAppIdAndAppSecret;
    }

    @Override
    public WeiboAccessTokenParam getAccessToken(String code) {
        return new WeiboAccessTokenParam(code);
    }

    @Override
    public OAuthAppIdAndAppSecret getOAuthInfo() {
        return weiboAppIdAndAppSecret;
    }

    /**
     * 获取AccessToken
     */
    @Override
    public WeiboUserShowResult getResultInfo(WeiboAccessTokenParam accessToken) {
        HttpParam httpParam = new HttpParam();
        httpParam.setMethod(RequestMethod.POST);
        httpParam.setUrl(ACCESS_TOKEN_URI);
        httpParam.addParam("client_id", weiboAppIdAndAppSecret.getAppId());
        httpParam.addParam("client_secret", weiboAppIdAndAppSecret.getAppSecret());
        httpParam.addParam("grant_type", accessToken.getGrant_type());
        httpParam.addParam("code", accessToken.getCode());
        httpParam.addParam("redirect_uri", weiboAppIdAndAppSecret.getRedirectUri());
        System.out.println(httpParam.getUri());
        HttpResult result = HttpClientUtil.request(httpParam);
        if (!result.isSuccess()) {
            throw new WeCatException(OAuthResultCode.ILLEGAL_ARGUMENT, "微博授权失败:访问接口失败");
        }
        String jsonResult = result.getContext();
        JSONObject json = JSONObject.parseObject(jsonResult);
        if (json.containsKey("error")) {
            throw new WeCatException(OAuthResultCode.ILLEGAL_ARGUMENT, "微博授权失败:" + json.getString("error"));
        }
        return JSONObject.parseObject(jsonResult, WeiboUserShowResult.class);
    }

    /**
     * 获取用户信息
     */
    @Override
    public WeiboUserShowResult getInfo(WeiboUserShowResult info) {
        HttpParam httpParam = new HttpParam();
        httpParam.setMethod(RequestMethod.GET);
        httpParam.setUrl(USER_SHOW_URI);
        httpParam.addParam("uid", info.getUid());
        httpParam.addParam("access_token", info.getAccess_token());
        HttpResult result = HttpClientUtil.request(httpParam);
        if (!result.isSuccess()) {
            throw new WeCatException(OAuthResultCode.ILLEGAL_ARGUMENT, "微博授权失败:访问接口失败");
        }
        String jsonResult = result.getContext();
        JSONObject json = JSONObject.parseObject(jsonResult);
        if (json.containsKey("error")) {
            throw new WeCatException(OAuthResultCode.ILLEGAL_ARGUMENT, "微博授权失败:" + json.getString("error"));
        }

        WeiboUserShowResult weiboUserShowResult = JSONObject.parseObject(jsonResult, WeiboUserShowResult.class);
        weiboUserShowResult.setAccess_token(info.getAccess_token());
        weiboUserShowResult.setExpires_in(info.getExpires_in());
        weiboUserShowResult.setRemind_in(info.getRemind_in());
        weiboUserShowResult.setUid(info.getUid());

        return weiboUserShowResult;
    }

    @Override
    public boolean supportsType(LoginType loginType) {
        return LoginType.WEIBO == loginType;
    }
}
